//
//  EGGoodsDetailController.m
//  Orchard
//
//  Created by 黄勇锋 on 16/8/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "EGGoodsDetailController.h"

@interface EGGoodsDetailController()

@property (strong,nonatomic) UIScrollView *scrollView;

@property (strong,nonatomic) UIImageView *detailImageView;

@property (strong,nonatomic) UIImageView *goodsImageView;

@property (strong,nonatomic) UIView *addGoodsView;

@property (strong,nonatomic) UILabel *addGoodsLabel;

@property (strong,nonatomic) UIButton *increaseButton;

@property (strong,nonatomic) UIButton *reduceButton;

@property (strong,nonatomic) UILabel *countLabel;

@property (strong,nonatomic) UIView *goodView;

@property (strong,nonatomic) UIView *productView;

@property (strong,nonatomic) UIImageView *shopCarImageView;

@property (strong,nonatomic) UIImageView *redCircelImageView;

@end



@implementation EGGoodsDetailController

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    self.title = @"果园老农灌装黄桃干";
    
    self.count = 0;
    
    if (self.index == 1) {
        
        self.title = self.freshArray.firstObject.name;
    }else if (self.index == 2){
        
        self.title = self.freshArray.lastObject.name;
    }
    
    //去掉导航栏返回按钮的文字
    [[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
    
    [self setupUI];
}


#pragma mark - setupUI
- (void)setupUI{
    

    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarbuttonClick)];
    
    //添加控件
    
    [self.view addSubview: self.scrollView];
    [self.view addSubview:self.addGoodsView];
    
    [self.addGoodsView addSubview:self.shopCarImageView];
    
    
    [self.scrollView addSubview:self.productView];
    [self.scrollView addSubview:self.detailImageView];
    [self.scrollView addSubview:self.goodView];
    
    
    [self.goodView addSubview:self.goodsImageView];
    
    [self.addGoodsView addSubview:self.addGoodsLabel];
    [self.addGoodsView addSubview:self.countLabel];
    [self.addGoodsView addSubview:self.reduceButton];
    [self.addGoodsView addSubview:self.increaseButton];
    
    //设置约束
    
    [self.productView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.goodView.bottom);
        make.bottom.equalTo(self.detailImageView.top);
        make.width.equalTo(375);
        make.height.equalTo(200);
    }];

    
    [self.addGoodsView makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(50);
    }];

    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.addGoodsView.top);
    }];
    
    [self.goodView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.equalTo(self.scrollView);
        make.width.equalTo(375);
        make.height.equalTo(200);
    }];

    [self.detailImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.productView.bottom);
        make.left.equalTo(self.scrollView);
        make.bottom.equalTo(self.scrollView);
        make.width.equalTo(375);
    }];
    
    [self.goodsImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(UIEdgeInsetsZero);
    }];

    [self.reduceButton makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.addGoodsLabel);
        make.left.equalTo(self.increaseButton.right).offset(32);
    }];

    [self.increaseButton makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.addGoodsLabel.mas_right).offset(10);
        make.centerY.equalTo(self.addGoodsLabel);
    }];
    

     // MARK:设置购物车
    [self.shopCarImageView makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.addGoodsView).offset(- 10);
        make.bottom.equalTo(self.addGoodsView).offset(- 10);
        
        make.size.equalTo(CGSizeMake(50, 50));
    }];

    
    [self.countLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.addGoodsLabel);
        make.left.equalTo(self.increaseButton.right).offset(10);
    }];
    
    [self.addGoodsLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.addGoodsView).offset(10);
        make.centerY.equalTo(self.addGoodsView);
    }];
}

#pragma mark - 右边导航按钮点击
- (void)rightBarbuttonClick{
    
    NSLog(@"rightBarbuttonClick");
}

#pragma mark - 赋值
- (void)setIndex:(NSInteger)index{
    
    _index = index;

}

- (void)setFreshArray:(NSArray *)freshArray{
    
    _freshArray = freshArray;
    
    if (self.index == 1) {
        
        [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:self.freshArray.firstObject.img]];
        
    }else if (self.index == 2){
        
        [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:self.freshArray.lastObject.img]];
    }
}


- (UIView *)addGoodsView{
    
    if (nil == _addGoodsView) {
        _addGoodsView = [[UIView alloc]init];
        
        _addGoodsView.backgroundColor = [UIColor orangeColor];
    }
    
    return _addGoodsView;
}

- (UILabel *)addGoodsLabel{
    
    if (nil == _addGoodsLabel) {
        _addGoodsLabel = [[UILabel alloc]init];
        _addGoodsLabel.text = @"添加商品:";
    }
    return _addGoodsLabel;
}



- (UILabel *)countLabel{
    
    if (_countLabel == nil) {
        _countLabel = [[UILabel alloc]init];
        
        _countLabel.text = [NSString stringWithFormat:@"%ld",self.count];
    }
    return _countLabel;
}

#pragma mark - reduceButton
- (UIButton *)reduceButton{
    
    if (_reduceButton == nil) {
        _reduceButton = [[UIButton alloc]init];
        [_reduceButton setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
        [_reduceButton sizeToFit];
        
        [_reduceButton addTarget:self action:@selector(reduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reduceButton;
}

- (void)reduceButtonClick{
    
    if (self.count > 0) {
        self.count--;
    }
    
    self.countLabel.text = [NSString stringWithFormat:@"%ld",self.count];
}

- (void)increaseButtonClick{
    
    self.count++;
    
    self.countLabel.text = [NSString stringWithFormat:@"%ld",self.count];
}

#pragma mark - increaseButton
- (UIButton *)increaseButton{
    
    if (_increaseButton == nil) {
        
        _increaseButton = [[UIButton alloc]init];
        
        [_increaseButton setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
        
        [_increaseButton sizeToFit];
        
        [_increaseButton addTarget:self action:@selector(increaseButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _increaseButton;
}

- (UIImageView *)goodsImageView{
    
    if (_goodsImageView == nil) {
        
        _goodsImageView = [[UIImageView alloc]init];
        
        _goodsImageView.backgroundColor = [UIColor greenColor];
    }

    return _goodsImageView;
}

- (UIScrollView *)scrollView{
    
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc]init];
        
        _scrollView.backgroundColor = [UIColor yellowColor];
        
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * 3);
    }
    return _scrollView;
}


- (UIImageView *)detailImageView{
    
    if (_detailImageView == nil) {
        
        _detailImageView = [[UIImageView alloc]init];
        _detailImageView.image = [UIImage imageNamed:@"aaaa"];
    }
    return _detailImageView;
}



#pragma mark - 购物车
- (UIImageView *)shopCarImageView{
    
    if (nil == _shopCarImageView) {
        
        _shopCarImageView = [[UIImageView alloc]init];
        
        _shopCarImageView.image = [UIImage imageNamed:@"shopCart_r"];
        
        _shopCarImageView.userInteractionEnabled = YES;
        

    }
    
    return _shopCarImageView;
}

- (UIView *)goodView{
    
    if (nil ==_goodView) {
        
        _goodView = [[UIView alloc]init];
        _goodView.backgroundColor = [UIColor redColor];
        
    }
    return _goodView;
}

- (UIView *)productView{
    
    if (_productView == nil) {
        _productView = [[UIView alloc]init];
        _productView.backgroundColor = [UIColor greenColor];
    }
    return _productView;
}

- (UIImageView *)redCircelImageView{
    
    if (nil == _redCircelImageView) {
        
        _redCircelImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_notice_dot"]];
    }
    
    return _redCircelImageView;
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
    
}



@end
